package com.hankol.message

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.BaseConstant
import com.baselibrary.Constants
import com.baselibrary.activity.BaseActivity
import com.baselibrary.bean.CommentEnum
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleClickHelper
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.message.adapter.CommentMeAdapter
import com.hankol.message.bean.CommentMeBean
import com.hankol.message.databinding.ActivityLikeAndCollectBinding
import com.hankol.message.viewmodel.HomeMessageViewModel
import com.okhttp.comment.CommentHelp
import java.util.*


/**评论我的
 * @author: merbng
 * @time: 2022/3/25 10:15
 * @description:
 */
@Route(path = ActivityRouter.MESSAGE.COMMENT_ME)
class CommentMeActivity : BaseActivity() {

    private lateinit var viewModel: HomeMessageViewModel
    private lateinit var binding: ActivityLikeAndCollectBinding
    val commentMeAdapter: CommentMeAdapter by lazy { CommentMeAdapter() }
    var page = 1
    private var hasnext: Boolean = false
    override fun getLayoutId(): Int {
        return R.layout.activity_like_and_collect
    }

    override fun init() {
        binding = getDataBinding()
        viewModel = getActivityViewModelProvider(this).get(HomeMessageViewModel::class.java)
        binding.tvTitle.text = "评论"
        binding.backImageView.setOnClickListener { finish() }
        binding.rvLike.adapter = commentMeAdapter
        commentMeAdapter.setOnItemClickListener { adapter, view, position ->
            if (!DoubleClickHelper.isOnDoubleClick()) {
                val bean = adapter.data[position] as CommentMeBean
                //动态类型：1-图文，2-视频
                if (bean.timelineShowtype == 1) {
                    val hashMap = HashMap<String, Any>()
                    hashMap["id"] = bean.timelineId
                    hashMap["type"] =if(bean.btype==1) CommentEnum.COMMENT_DYNAMIC.commentType else CommentEnum.COMMENT_CHILDREN.commentType
                    hashMap["isOpenComment"] = "true"
                    hashMap["commentId"] = bean.taCommentId //评论id，高亮评论的那条
                    hashMap["source"] = BaseConstant.POINT_DYNAMIC_COM_MY
                    hashMap["typeId"] = "${if(bean.btype==1)bean.timelineId else bean.myCommentId}"
                    ARouterUtils.jump(ActivityRouter.ACTIVITY.IMAGE_DETAIL, hashMap)
                } else { //视频动态
                    val hashMap = HashMap<String, Any>()
                    hashMap["id"] = bean.timelineId
                    hashMap["bid"] = bean.timelineId
                    hashMap["isOpenComment"] = "true"
                    hashMap["JumpType"] = 0
                    hashMap["commentId"] = bean.taCommentId //评论id，高亮评论的那条
                    hashMap["source"] = BaseConstant.POINT_DYNAMIC_COM_MY
                    hashMap["type"] =if(bean.btype==1) CommentEnum.COMMENT_DYNAMIC.commentType else CommentEnum.COMMENT_CHILDREN.commentType
                    hashMap["typeId"] = "${if(bean.btype==1)bean.timelineId else bean.myCommentId}"
                    ARouterUtils.jump(ActivityRouter.ACTIVITY.READ_VIDEO_DETAIL, hashMap)
                }
            }
        }

        commentMeAdapter.setOnItemChildClickListener { adapter, view, position ->

            val bean = adapter.data[position] as CommentMeBean
            when (view.id) {
                R.id.tv_reply -> {
                    if (!DoubleClickHelper.isOnDoubleClick()) {
                        //回复
                        openReplyPop(bean)
                    }
                }
                R.id.img_heard, R.id.ll_nick -> {
                    if (!DoubleClickHelper.isOnDoubleClick()) {
                        val hashMap = HashMap<String, Any>()
                        hashMap[BaseConstant.USER_ID] = bean.uid
                        ARouterUtils.jump(ActivityRouter.MINE_CENTER.USER_INFO, hashMap)
                    }
                }
            }

        }
        binding.smartRefresh.setOnRefreshListener {
            page = 1
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }
        observerCallBck()
        loadData()
        commentMeAdapter.setLikeCallBack(object : CommentMeAdapter.LikeCallBack {
            override fun likeClick(pos: Int) {
                val bean = commentMeAdapter.data[pos]
                likeOrUnlike(bean,pos)
            }

            override fun unLikeClick(pos: Int) {
                val bean = commentMeAdapter.data[pos]
                likeOrUnlike(bean,pos)
            }
        })
        //获取页面开始时间
        SensorsDataManager.instance.onCreateTime()
    }

    val mCommentHelp = ActivityRouter.getARouterInterface<CommentHelp>(ActivityRouter.COMMENT.COMMENT_HELP)

    private fun openReplyPop(bean: CommentMeBean) {
        SensorsDataManager.instance.commentMeQuick(bean.name, false)
        BaseConstant.loadCommentEventBusBean("动态", "快捷评论页")
        val intent = Intent(this, mCommentHelp.windowAddComment)
        val bundle = Bundle()
        bundle.putInt("type", 1)
        bundle.putBoolean("isShowReply", true)
        bundle.putInt("typeId", bean.taCommentId)
//        bundle.putInt("pId", bean.commentPid)
//        bundle.putInt("puId", bean.uid)
        bundle.putString("title", bean.name)
        intent.putExtras(bundle)
        startActivity(intent)
    }

    fun likeOrUnlike(bean: CommentMeBean,pos:Int) {
        SensorsDataManager.instance.commentMeQuick(bean.name, true)
        //点赞&取消点赞
        //点赞类型 1评论的点赞 2文章点赞 3点评点赞 4动态  6课程 7直播间
        viewModel.reqSupport(bean.isSupport == 0, 1, bean.taCommentId)
        bean.isSupport = if (bean.isSupport == 1) 0 else 1
    }

    private fun observerCallBck() {
        viewModel.commentMedataListLD.observe(this, Observer {
            hideLoadingDialog()
            binding.smartRefresh.finishRefresh()
            if (it == null || it.data?.size == 0) {
                //无数据
                if (page == 1) {
                    commentMeAdapter.emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_LIKE_COMMENT, binding.rvLike, "无相关评论信息")
                } else {
                    binding.smartRefresh.finishLoadMore()
                }
            } else {
                if (it.data != null) {
                    hasnext = it.totalPage != commentMeAdapter.data.size
                    binding.smartRefresh.finishLoadMore()
                    if (page == 1) {
                        commentMeAdapter.setNewData(it.data)
                    } else {
                        commentMeAdapter.addData(it.data)
                    }
                    page++
                }
            }
        })
    }

    fun loadData() {
        showLoadingDialog()
        viewModel.reqCommentMeList(page)
    }

    override fun onDestroy() {
        super.onDestroy()
        //获取页面结束时间
        SensorsDataManager.instance.onDestroyTime()
        if (SensorsDataManager.instance.getBrowseTime() != 0L) {
            SensorsDataManager.instance.brookPageView("评论我的")
        }
    }
}